﻿using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Meeting.DAL;
using XF.Meeting.DalFactory;
using XF.Meeting.Entity.DTO;
using XF.Meeting.IDal;

namespace EWS
{
    public class DbToExchangeService
    {
        static IExchangeToDbDal dal = new ExchangeToDbDal();
        public static EmailEntity getEmailEntity(MeetingOrder order)
        {
            EmailEntity entity= new EmailEntity();

            entity.Subject = order.ConferenceTheme;
            entity.Body = order.ConferenceRemark;
            entity.Start = order.BeginTime;
            entity.End = order.EndTime;
            entity.Location = string.Empty;

            string[] ParticipantIDs = order.ParticipantIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] CRoomIDs = order.CRoomIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in ParticipantIDs)
            {
                var users = dal.GetUserEmail(Convert.ToInt32(item));
                Attendeee attendee = new Attendeee(users.Mail, users.Name);

                entity.AttendeesList.Add(attendee);
            }
            for (int i = 0; i < CRoomIDs.Length; i++)
            {
                
                var room = dal.GetRoomrMailByID(Convert.ToInt32(CRoomIDs[i]));
                if(CRoomIDs.Length>1)
                {
                    entity.Location += room.Name + ",";
                }
                else
                {
                    entity.Location += room.Name;
                }
                Attendeee attendee = new Attendeee(room.Mail, room.Name);
                
                entity.AttendeesList.Add(attendee);
            }
                
            entity.Subject = order.ConferenceTheme;

            return entity;
        }
        public static bool DeleteRecurringItem(ExchangeService service, Appointment recurringItem)
        {
            Appointment appointmentToDelete = null;
            // If the item is a single appointment, fail.
            if (recurringItem.AppointmentType == AppointmentType.Single)
            {
                return false;
            }
                    // The item passed is not the recurring master, but the caller
                    // wants to delete the entire series. Bind to the recurring
                    // master to delete it.
                    try
                    {
                        appointmentToDelete = Appointment.BindToRecurringMaster(service, recurringItem.Id);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR: {0}", ex.Message);
                        return false;
                    }

            
            if (appointmentToDelete != null)
            {
                // Remove the item, depending on the scenario. 

                    // The item isn't a meeting, so just delete it.
                    appointmentToDelete.Delete(DeleteMode.MoveToDeletedItems);
                    return true;
                
            }
            return false;
        }
    }
}
